const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemsid:0,
    // domain:Api.domain,
    ios:app.globalData.ios,//是否为ios
    items:'',
    integral_pay:'',//购买积分数量
    show:false,//是否显示激活卡弹框
    zhu:'',
    userinfo:'',
    num:'',//1元对应的积分数量
    show2:false,//如何领取激活码
    can:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init()
    this.setData({
      can:wx.$cache.get('can'),
      num:wx.$cache.get('can').integral_price_proportion,
      ios: app.globalData.ios,
    })
    this.mywo()
  },
  qie(e){
    let {index} = e.currentTarget.dataset
    this.setData({
      itemsid:index,
      integral_pay:''
    })
  },
  btn2(){//获取激活码弹框
    this.setData({
      show2:true
    })
  },
  mywo(){//我的
    var that = this
    wx.$http.get(wx.$get.get_member_info,{}).then(res=>{
      if(!res.vip_time){
        res.vip_time='1970-01-01'
      }
      console.log('我的积分', res)
      that.setData({
        userinfo:res
      })
      wx.$cache.set('userinfo',res)
    })
  },
  init(){//获取套餐数
    var that = this
    var params = {
      
    }
    wx.$http.get(wx.$get.integral_meal,params).then(res=>{
      console.log('获取套餐数', res)
      that.setData({
        items:res
      })
    })
  },
  show(){//激活码弹框出来
    this.setData({
      show:true
    })
  },
  btn(){//立即支付
    var that = this
    var params = {
     type:this.data.integral_pay?3:1,
     meal_id:this.data.integral_pay?'':this.data.items[this.data.itemsid].id,
      integral_pay:this.data.integral_pay
    }
    wx.$http.get(wx.$get.pay_integral_meal,params,'支付...').then(ress=>{
      wx.requestPayment({
        timeStamp: ress.timeStamp,
        nonceStr: ress.nonceStr,
        package: ress.package,
        signType: 'MD5',
        paySign: ress.paySign,
        success(res) {
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 2000
          })
          that.mywo()
        },
        fail(res) {
          
        }
      })
    })
  },
  zhi(e){//激活码兑换
    let code = e.detail
    var that = this
    var params = {
      type:2,
      code
     }
     wx.$http.get(wx.$get.pay_integral_meal,params).then(res=>{
      console.log('获取套餐数', res)
      that.setData({
        show:false
      })
      wx.showToast({
       title: '兑换成功',
       icon: 'success',
       duration: 2000
     })
      that.mywo()
     })
  },
  bindchinge(e){
    this.setData({
      integral_pay:e.detail.value,
      itemsid:null
    })
  },
  onPageScroll(e) { //监听屏幕滚动
    if(e.scrollTop>80){
      this.setData({
        background:'white'
      })
    }else{
      this.setData({
        background:''
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})